import { ref, reactive, computed } from 'vue';
import FileBasicInfoForm from './components/FileBasicInfoForm.vue';
import FileApplicationScopeForm from './components/FileApplicationScopeForm.vue';
import FilePropertyForm from './components/FilePropertyForm.vue';
import FileUploadForm from './components/FileUploadForm.vue';
import FileReceiptRangeForm from './components/FileReceiptRangeForm.vue';
import FileAuditForm from './components/FileAuditForm.vue';
import FileAssociation from './components/FileAssociation.vue'; // 关联关系
import FileQualityRecords from './components/FileQualityRecords.vue'; // 质量记录一览表

// 当前步骤条值
export const stepsCurrentValue = ref(0);

// 整体表单数据对象
export const formData = reactive({
    // 文件基本信息
    basicInfo: {
        fileCategory: '2', // 文件大类【默认 '体系文件'】
        filesClassId: '',
    },
    // 适用范围
    applicationScope: {},
    // 文件属性信息
    property: {},
    // 上传附件
    attachmentFile: {},
    // 发文范围
    receiptRange: {
        receiptRange: 'Western QMS', // 发文范围【默认值】
    },
    // 审批人
    auditUser: {},
    // 关联关系
    association: {},
    // 质量记录一览表
    qualityRecords: {},
});
// 基础步骤条配置
const baseStepsItems = [
    {
        title: '文件基本信息',
        description: '文件基本信息',
        formComponent: FileBasicInfoForm,
        formData: 'basicInfo',
    },
    {
        title: '适用范围',
        description: ' ',
        formComponent: FileApplicationScopeForm,
        formData: 'applicationScope',
    },
    {
        title: '文件属性信息',
        description: ' ',
        formComponent: FilePropertyForm,
        formData: 'property',
    },
    {   
        // 文件大类：临时文件时展示
        title: '关联关系',
        description: '',
        show: false,
        formComponent: FileAssociation,
        formData: 'association',
    },
    {   
        // 文件大类: 体系文件， 文件类别：质量记录时 "质量记录一览表"
        title: '质量记录一览表',
        description: '',
        show: false,
        formComponent: FileQualityRecords,
        formData: 'qualityRecords',
    },
    {
        title: '上传附件',
        description: ' ',
        formComponent: FileUploadForm,
        formData: 'attachmentFile',
    },
    {
        title: '发文范围',
        description: ' ',
        formComponent: FileReceiptRangeForm,
        formData: 'receiptRange',
    },
    {   // 文件大类：外来文件时 不展示
        title: '审批人',
        description: ' ',
        formComponent: FileAuditForm,
        formData: 'auditUser',
    },
]
// 步骤条数据
export const stepsItems = computed(() => {
    let list = [];
    const fileCategory = formData?.basicInfo?.fileCategory;
    const filesClassId = formData?.basicInfo?.filesClassId;
    const fileCategoryShowMap = {
        '2': ['文件基本信息', '适用范围', '文件属性信息', '上传附件', '发文范围', '审批人'],
        '1': ['文件基本信息', '适用范围', '文件属性信息', '关联关系', '上传附件', '发文范围', '审批人'],
        '4': ['文件基本信息', '适用范围', '文件属性信息',  '上传附件', '发文范围'],
    }
    let showList = fileCategoryShowMap[fileCategory];
    // 文件大类: 体系文件， 文件类别：质量记录时 "上传附件" => "质量记录一览表"
    if ('137' === String(filesClassId)) {
        showList = showList.map((item) =>{
            if ('上传附件' === item) item = '质量记录一览表';
            return item;
        });
    }
    console.log('showList=>', showList)
    if (Array.isArray(showList)) {
        list = baseStepsItems.filter((item) => showList.includes(item.title));
    }
    
    console.log('list.value=>', list)
    return list;
});

// export const updateStepsItems = () =>{
//     const fileCategory = formData?.basicInfo?.fileCategory;
//     const fileCategoryShowMap = {
//         '2': ['文件基本信息', '适用范围', '文件属性信息', '上传附件', '发文范围', '审批人'],
//         '1': ['文件基本信息', '适用范围', '文件属性信息', '关联关系', '上传附件', '发文范围', '审批人'],
//         '4': ['文件基本信息', '适用范围', '文件属性信息',  '上传附件', '发文范围'],
//     }
//     const showList = fileCategoryShowMap[fileCategory];
//     console.log('showList=>', showList)
//     if (Array.isArray(showList)) {
//         stepsItems.value = baseStepsItems.filter((item) => showList.includes(item.title));
//     }
//     console.log('stepsItems.value=>', stepsItems.value);
// }
// 当前步骤条项
export const currentStepItem = computed(() => {
    return stepsItems.value[stepsCurrentValue.value];
});